package com.controller;

import com.dto.BookDTO;
import com.dto.DonationDTO;
import com.service.DonationServiceImpl;
import com.service.LoginServiceImpl;
import com.util.Result;
import com.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linyukun
 * @date 2022/10/8 10:52
 */
@RequestMapping("donation")
@RestController
public class DonationController {
    @Autowired
    private DonationServiceImpl donationServiceImpl;

    @Autowired
    private LoginServiceImpl loginServiceImpl;

    /**
     * 新增赠书信息
     * @author linyukun
     * @date 2022-10-08 10:58:00
     */
    @RequestMapping(value="/addDonation",method = RequestMethod.POST)
    public Result addDonation(@RequestBody DonationDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setAddUser(userName);
            return ResultUtil.returnDataSuccess(donationServiceImpl.addDonation(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 删除赠书信息
     * @author linyukun
     * @date 2022-10-08 11:00:00
     */
    @RequestMapping(value="/delDonation",method = RequestMethod.POST)
    public Result delDonation(@RequestBody DonationDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setDeleteUser(userName);
            return ResultUtil.returnDataSuccess(donationServiceImpl.delDonation(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 修改赠书信息
     * @author linyukun
     * @date 2022-10-08 11:00:00
     */
    @RequestMapping(value="/updateDonation",method = RequestMethod.POST)
    public Result updateDonation(@RequestBody DonationDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setUpdateUser(userName);
            return ResultUtil.returnDataSuccess(donationServiceImpl.updateDonation(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取赠书信息列表
     * @author linyukun
     * @date 2022-10-08 11:05:00
     */
    @RequestMapping(value="/getDonationList",method = RequestMethod.POST)
    public Result getDonationList(@RequestBody DonationDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(donationServiceImpl.getDonationList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 获取所有的赠书的日期
     * @author linyukun
     * @date 2022-10-08 18:50:00
     */
    @RequestMapping(value="/getAllDonationDate",method = RequestMethod.POST)
    public Result getAllDonationDate(){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(donationServiceImpl.getAllDonationDate());
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

}
